package cn.hs.ph.dao.impl;

import cn.hs.ph.dao.ShopDao;
import cn.hs.ph.entity.Shop;
import cn.hs.ph.util.JDBCDruidUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

public class ShopDaoimpl implements ShopDao {
    @Override
    public int insert(Shop shop) throws SQLException {
        String sql="insert into shop values(null,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        Object obj[]={
                shop.getShopName(),
                shop.getShopDescribe(),
                shop.getShopImage(),
                shop.getShopPhone(),
                shop.getShopProvince(),
                shop.getShopCity(),
                shop.getShopDistrict(),
                shop.getShopAddress(),
                shop.getShopLongitude(),
                shop.getShopLatitude(),
                shop.getShopStatus(),
                new Date(),
                shop.getShopUserId(),
                shop.getShopAccount(),
                shop.getShopPassword(),
                shop.getShopSort(),
                shop.getSalesVolume(),
                shop.getDistance(),
                shop.getShopCommunitySort(),
                shop.getConcernStatus(),
                shop.getShopOrderCount()


        };
        return JDBCDruidUtil.update(sql,obj);

    }

    @Override
    public int delete(int shopId) throws SQLException {
        String sql="delete from shop where shopId=?";
        Object[] obj={shopId};
       return JDBCDruidUtil.update(sql,obj);



    }

    @Override
    public int update(Shop shop) throws SQLException {
        String sql="update shop set shopAddress=?, shopCity=?,shopDescribe=?,shopDistrict=?,shopImage=?, shopLatitude=?,shopLongitude=?,shopName=?, shopPhone=?, shopProvince=? where shopId=?";
Object obj[]={
        shop.getShopAddress(),
        shop.getShopCity(),
        shop.getShopDescribe(),
        shop.getShopDistrict(),
        shop.getShopImage(),
        shop.getShopLatitude(),
        shop.getShopLongitude(),
        shop.getShopName(),
        shop.getShopPhone(),
        shop.getShopProvince(),
        shop.getShopId()
};

        return JDBCDruidUtil.update(sql,obj);
    }

    @Override
    public List<Shop> selectAll(Map<String, String> map) throws SQLException {
        List<Shop> list = new ArrayList<Shop>();
        String sql = "select * from shop where 1=1 ";
        List params = new ArrayList();
        String shopName = map.get("shopName");
        if (shopName != null && !"".equals(shopName)) {
            sql += "  and shopName like ? ";
            params.add("%" + shopName + "%");

        }
        String startTime = map.get("startTime");
        if (startTime!=null&&!startTime.equals("")) {
            sql+=" and createTime between ? and ?";
            params.add(startTime);
            params.add(map.get("endTime"));
        }
        String pn = map.get("pageNum");
        if (pn!=null&&!pn.equals("")) {
            sql+=" limit ?,? ";
            int pageNum = Integer.parseInt(pn);
            int pageSize = Integer.parseInt(map.get("pageSize"));
            params.add((pageNum-1)*pageSize);
            params.add(pageSize);
        }
        Object[] array = params.toArray();
        ResultSet query = JDBCDruidUtil.query(sql, array);
        while (true) {
            if (!query.next()) break;
            Shop shop = new Shop(
                    query.getInt(1),
                    query.getString(2),
                    query.getString(3),
                    query.getString(4),
                    query.getString(5),
                    query.getString(6),
                    query.getString(7),
                    query.getString(8),
                    query.getString(9),
                    query.getString(10),
                    query.getString(11),
                    query.getInt(12),
                    query.getDate(13),
                    query.getInt(14),

                    query.getString(15),
                    query.getString(16),
                    query.getString(17),
                    query.getString(18),

                    query.getDouble(19),

                    query.getString(20),
                    query.getString(21),

                    query.getInt(22)

            );
            list.add(shop);


        }
JDBCDruidUtil.close();
        return list;





    }

    @Override
    public int selectCount(Map<String, String> map) throws SQLException {
        int count = 0;

        String sql = "select count(1) from shop where 1=1 ";
        List params = new ArrayList();
        String shopName = map.get("shopName");
        if (shopName != null && !"".equals(shopName)) {
            sql += "  and shopName like ? ";
            params.add("%" + shopName + "%");

        }
        String startTime = map.get("startTime");
        if (startTime!=null&&!startTime.equals("")) {
            sql+=" and createTime between ? and ?";
            params.add(startTime);
            params.add(map.get("endTime"));
        }

        Object[] array = params.toArray();
        ResultSet query = JDBCDruidUtil.query(sql, array);
        while (true){

            if (!query.next()) break;
            count=query.getInt(1);
        }
        JDBCDruidUtil.close();
        return count;

    }
}
